package 富婆梦.第一阶段.第三周.作业;

import 富婆梦.第一阶段.第三周.作业.MyArrayList;

public class MyLinkList {
    private int length=0;
    private Object[] object = new Object[length];

    public int getLength(){
        return length;
    }
    //get
    public Object getLast(){
        return get(length-1);
    }
    public Object getFirst(){
        return get(0);
    }
    public int lastIndexOf(Object object){
        return 0;
    }
    public Object get(int index) {
        if (index > length - 1) {
            System.out.println("数组越界!");
            return null;
        } else {
            return object[index];
        }
    }
    public int size() {
        return object.length;
    }
    public int indexOf(Object element) {
        for (int i = 0; i < object.length; i++) {
            if (object[i] == element) {
                return i;
            }
        }
        return -1;
    }
    public boolean isEmpty() {
        if (length == 0) {
            return true;
        }
        return true;
    }

    //add
    public Object[] addFirst(Object element){
        length = length + 1;
        Object[] x = new Object[length];
        x[0] = element;
        for (int i = 1; i < length; i++) {
            x[i] = object[i-1];
        }
        object = x;
        return object;
    }
    public Object[] addLast(Object element){
        length = length + 1;
        Object[] x = new Object[length];
        x[length-1] = element;
        for (int i = 0; i < length-1; i++) {
            x[i] = object[i];
        }
        object = x;
        return object;
    }
    public Object[] add(Object element) {
        length = length + 1;
        Object[] x = new Object[length];
        x[length - 1] = element;
        for (int i = 0; i < length - 1; i++) {
            x[i] = object[i];
        }
        object = x;
        return object;
    }
    public Object[] add(int index, Object element) {
        if (index > length) {
            System.out.println("数组越界！操作失败！！！");
        } else {
            length = length + 1;
            Object[] x = new Object[length];
            for (int i = 0; i < index; i++) {
                x[i] = object[i];
            }
            x[index] = element;
            for (int i = index; i < length - 1; i++) {
                x[i + 1] = object[i];
            }
            object = x;
        }
        return object;
    }

    //remove
    public Object[] removeFirst(){
        length = length - 1;
        Object[] x = new Object[length];
        for (int i = 0; i < length; i++) {
            x[i] = object[i+1];
        }
        object = x;
        return object;
    }
    public Object[] removeLast(){
        length = length - 1;
        Object[] x = new Object[length];
        for (int i = 0; i < length; i++) {
            x[i] = object[i];
        }
        object = x;
        return object;
    }

    public Object[] remove(int index) {
        if (index > length - 1 || index < 0) {
            System.out.println("数组越界！操作失败！！！");
        } else {
            length = length - 1;
            Object[] x = new Object[length];
            for (int i = 0; i < index; i++) {
                x[i] = object[i];
            }
            for (int i = index; i < length; i++) {
                x[i] = object[i + 1];
            }
            object = x;
        }
        return object;
    }

    public Object[] remove(Object element) {
        int index = 0;
        for (int i = 0; i < length; i++) {
            if (object[i] == element) {
                index = i;
            }
        }
        return remove(index);
    }

}
